'************************************************************************************
' Copyright  2003-2005 Kathleen Dollard
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the author be held liable for any damages arising from the use of this
' software.
' 
' You may use this software for any purpose, including commercial applications, 
' and you may redistribute this software as part of application delivery: however, 
' this copyright notice may not be removed and you may not otherwise redistribute 
' this software directly other than providing a link to wwww.gendotnet.com. 
'
' You may alter this software to better server your needs (I'd like to hear about your 
' changes in order to make the software better). If you alter the softwre, you must  
' clearly mark it as altered and must not be misrepresented as the original software.
' nor wholly your original work.
'*************************************************************************************
'  Summary: Contains some general use functions 

Option Strict On
Option Explicit On 

Imports System
Imports System.Diagnostics

Namespace KADGen.Utility
   Public MustInherit Class SourceControlBase
      Implements IDisposable

      Public Enum ItemStatus
         Unknown = 0
         PreviouslyCheckedOutToMe
         NowCheckedOutForGen
         NotCheckedOut
         NotCheckedOutToMe
         DoesntExist
         NoSourceControl
         CheckoutNotRequested
      End Enum

      Private Shared mSourceControlInformation As ISourceControlInformation

#Region "MustOverrides"
      Public MustOverride Function CheckOut( _
                        ByVal file As String, _
                        ByVal basePath As String, _
                        ByVal baseProject As String) _
                        As SourceControlBase.ItemStatus

      Public MustOverride Function CheckIn( _
                        ByVal file As String, _
                        ByVal basePath As String, _
                        ByVal baseProject As String, _
                        ByVal itemStatus As Utility.SourceControlBase.ItemStatus, _
                        ByVal addFile As Boolean) _
                        As Utility.SourceControlBase.ItemStatus

      Public MustOverride Sub AddFile( _
                        ByVal file As String, _
                        ByVal basePath As String, _
                        ByVal baseProject As String)

      Public MustOverride Function GetItemStatus( _
                        ByVal file As String, _
                        ByVal basePath As String, _
                        ByVal baseProject As String) _
                        As ItemStatus

      Public MustOverride Sub CleanUp()
#End Region

#Region "Disposal Stuff"
      Public Overridable Sub Dispose() _
                        Implements System.IDisposable.Dispose
         CleanUp()
         GC.SuppressFinalize(Me)
      End Sub

      Protected Overrides Sub Finalize()
         CleanUp()
      End Sub
#End Region

#Region "Methods"
      Public Shared Property SourceControlInformation() As ISourceControlInformation
         Get
            Return mSourceControlInformation
         End Get
         Set(ByVal value As ISourceControlInformation)
            mSourceControlInformation = value
         End Set
      End Property

      Public Shared Function GetSourceControl(ByVal dllPath As String) As SourceControlBase
         If SourceControlInformation Is Nothing Then
            Return Nothing
         End If
         Dim ssType As String = SourceControlInformation.SourceControlPath
         Dim ssPath As String = SourceControlInformation.SourceControlType
         If String.IsNullOrEmpty(ssType) Then
            Return Nothing
         Else
            ' Cheap and dirty way to get part after last period. Assume the dll is the ssName plus "Tools"
            Dim ssDllName As String = IO.Path.GetExtension(ssType).Substring(1) & "Tools.dll"
            Dim type As System.Type
            Dim obj As Object
            Dim constructorInfo As Reflection.ConstructorInfo
            Dim path As String
            Dim sourceControl As SourceControlBase = Nothing
            Try
               ' I am opening this via reflection so people that don't have SourceSafe
               ' can run this tool. Providing an alternate assembly with the same naming
               ' will open
               If Not ssType Is Nothing AndAlso ssType.Trim.Length > 0 Then
                  Dim asm As Reflection.Assembly
                  path = IO.Path.Combine(dllPath, ssDllName)
                  asm = Reflection.Assembly.LoadFile(path)
                  If Not asm Is Nothing Then
                     type = asm.GetType(ssType)
                     If Not type Is Nothing Then
                        constructorInfo = type.GetConstructor(New System.Type() {GetType(String)})
                        If Not constructorInfo Is Nothing Then
                           obj = constructorInfo.Invoke(New Object() {ssPath})
                           sourceControl = CType(obj, Utility.SourceControlBase)
                        End If
                     End If
                  End If
               End If
               Return sourceControl

            Catch ex As Exception
               Diagnostics.Debug.WriteLine("No SourceSafe")
            End Try
         End If
         Return Nothing
      End Function

      Public Overridable Function BuildSSPath( _
                        ByVal file As String, _
                        ByVal basePath As String, _
                        ByVal baseProject As String) _
                        As String
         'file = IO.Path.GetFullPath(file)
         'file = file.Replace(basePath, "")
         'If file.StartsWith("/") Or file.StartsWith("\") Then
         '   file = file.Substring(1)
         'End If
         file = IO.Path.GetFileName(file)
         Return IO.Path.Combine(baseProject, file).Replace("\", "/")
      End Function

#End Region

   End Class

#Region "Source Control Exceptions"
   Public Class SourceControlException
      Inherits System.ApplicationException

      Private mFile As String

      Public Sub New(ByVal message As String, _
                        ByVal file As String)
         MyBase.New(message)
         mFile = file
      End Sub

      Public Sub New(ByVal message As String, _
                        ByVal innerException As System.Exception, _
                        ByVal file As String)
         MyBase.New(message, innerException)
         mFile = file
      End Sub

      Public Property File() As String
         Get
            Return mFile
         End Get
         Set(ByVal Value As String)
            mFile = Value
         End Set
      End Property

   End Class
#End Region
End Namespace